//
//  ViewController.m
//  kankanVideo
//
//  Created by wangbo on 5/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "ListItem.h"
#import "CXMLDocument.h"
#import "CXMLElement.h"

@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    /*ListItem *item=[[ListItem alloc] initWithFrame:CGRectMake(6, 0, 150, 110)];
    [self.view addSubview:item];
    ListItem *item1=[[ListItem alloc]initWithFrame:CGRectMake(164, 0, 150, 110)];
    [self.view addSubview:item1];*/
    NSString *xmlpath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"xmlfile.xml"];
    NSData *data=[NSData dataWithContentsOfFile:xmlpath];
    CXMLDocument *doc=[[CXMLDocument alloc] initWithData:data options:0 error:nil];
    [self parseRoot:doc];
    NSLog(@"abcdefg");
}

- (void) parseRoot:(CXMLDocument *) document
{
    CXMLElement *root = [document rootElement];
    NSArray *books = [root children];
    
    for (CXMLElement *element in books)
    {
        if ([element isKindOfClass:[CXMLElement class]])
        {
            NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
            for (int i = 0; i < [element childCount]; i++)
            {
                if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]])
                {
                    if ([[element name] isEqualToString:@"iphonebook"]) 
                    {
                        [item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name]];
                        NSLog(@"1%@", [[element childAtIndex:i] stringValue] );
                    }
                    if ([[element name] isEqualToString:@"wp7book"]) 
                    {
                        [item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name]];
                        NSLog(@"2%@", [[element childAtIndex:i] stringValue] );
                    }
                }
            }
        }
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
